package com.ulic.customerManApplication.commonmodule.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.commonmodule.bo.Organization;
import com.ulic.customerManApplication.commonmodule.dao.OrganizationDao;
import com.ulic.customerManApplication.commonmodule.service.OrganizationService;
import com.ulic.customerManApplication.usermodule.bo.User;

@Component("organizationService")
public class OrganizationServiceImpl implements OrganizationService {

	private OrganizationDao organizationDao;
	
	@Resource
	public void setOrganizationDao(OrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
	}


	@Override
	public List<Organization> getOrganizations() {
		// TODO Auto-generated method stub
		
		return organizationDao.getOrganizations();
	}


	@Override
	public Organization getOrganizationByFullName(String fullName) {
		// TODO Auto-generated method stub
		return organizationDao.getOrganizationByFullName(fullName);
	}


	@Override
	public List<Organization> getOrganizations(String roleName,User user) {
		return organizationDao.getOrganizations(roleName, user);
	}


	/**
	 * Description: 获得终极父机构
	 * @Version1.0 
	 * 2012-9-7 下午05:02:43 李求智 (liqz@huilan.com) created
	 * @return
	 */
	@Override
	public Organization getFinalOrganization() {
		return organizationDao.getFinalOrganization();
	}


	/**
	 * Description: 根据parentid查找子机构
	 * @Version1.0 
	 * 2012-9-7 下午05:02:45 李求智 (liqz@huilan.com) created
	 * @param parentId
	 * @return
	 */
	@Override
	public List<Organization> findOrganizationByParentId(String parentId) {
		return organizationDao.findOrganizationByParentId(parentId);
	}


	/**
	 * Description: 判断机构是否包含用户
	 * @Version1.0 
	 * 2012-9-7 下午05:15:29 李求智 (liqz@huilan.com) created
	 * @param id
	 * @return
	 */
	@Override
	public boolean organIncludeUser(String id) {
		return organizationDao.organIncludeUser(id);
	}


	/**
	 * Description: 判断是否是初级部门
	 * @Version1.0 
	 * 2012-9-12 下午05:03:52 李求智 (liqz@huilan.com) created
	 * @param id
	 * @return
	 */
	@Override
	public boolean organIsExpensed(String id,String parentid) {
		return organizationDao.organIsExpensed(id,parentid);
	}


	/**
	 * Description: 查询用户所在的分公司
	 * @Version1.0 
	 * 2012-9-12 下午05:03:40 李求智 (liqz@huilan.com) created
	 * @param userOid
	 * @return
	 */
	@Override
	public Organization getBranchByUserOid(String organOid) {
		return organizationDao.getBranchByUserOid(organOid);
	}


	/**
	 * Description: 查询用户所在的中支
	 * @Version1.0 
	 * 2012-9-12 下午05:03:31 李求智 (liqz@huilan.com) created
	 * @param userOid
	 * @return
	 */
	@Override
	public Organization getExpensedByUserOid(String organOid,String organOid1) {
		return organizationDao.getExpensedByUserOid(organOid,organOid1);
	}


	/**
	 * Description: 加载机构树
	 * @Version1.0 
	 * 2012-9-17 上午11:10:40 李求智 (liqz@huilan.com) created
	 */
	@Override
	public void reloadOrganizationTree() {
		try {
			URL path = this.getClass().getResource("/ReportTree.properties");
			System.out.println("path========================"+path.getPath());
			File file = new File(path.getPath().replaceAll("%20", " "));
			Writer write = new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"); 
			Organization organ = getFinalOrganization();
			StringBuffer sb = new StringBuffer();
			sb.append(organ.getUm_organ_id()).append(",").append("-1").append(",").append(organ.getFull_name()).append(",").append("false").append(",").append("false");
			write.write(sb.toString()+"\n");
			reloadSubOrganization(write,organ.getUm_organ_id());
			write.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Description: 获得子机构
	 * @Version1.0 
	 * 2012-9-17 下午01:14:56 李求智 (liqz@huilan.com) created
	 * @param write
	 * @param parentOid
	 */
	private String parentid = null;
	public void reloadSubOrganization(Writer write,String parentOid){
		try {
			List<Organization> organList = findOrganizationByParentId(parentOid);
			
			if(organList!=null&&organList.size()>0){
		loop:		for(int i=0;i<organList.size();i++){
					Organization organ = organList.get(i);
					String organname = organ.getFull_name().replaceAll("合众人寿保险股份有限公司","").replaceAll("合众人寿","");
					StringBuffer sb = new StringBuffer();
					StringBuffer stringBuffer = new StringBuffer();
					if(organname.indexOf("分公司")>0&&!organname.endsWith("分公司")){
						organname = organname.substring(organname.indexOf("分公司")+3);
					}
					if(organname.endsWith("分公司")){
						sb.append(organ.getUm_organ_id()).append(",").append(parentOid).append(",").append(organname).append(",").append("false").append(",").append("false");
						write.write(sb.toString()+"\n");
						parentid = organ.getUm_organ_id();
					}else if(parentid!=null&&organIsExpensed(organ.getUm_organ_id(),parentid)){
						sb.append(organ.getUm_organ_id()).append(",").append(parentOid).append(",").append(organname).append(",").append("false").append(",").append("true").append(",");
						getOrgansBySubCityId(organ.getUm_organ_id());
						if(organs.size()>0){
							for(Organization organization:organs){
								stringBuffer.append(organization.getUm_organ_id()+":");
							}
							stringBuffer.append(organ.getUm_organ_id());
						    organs = new ArrayList<Organization>();
						}
						sb.append("/user/list.do?organOid="+stringBuffer.toString());
						write.write(sb.toString()+"\n");
					}else{
						continue loop;
					}
					reloadSubOrganization(write,organ.getUm_organ_id());
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	@Override
	public Organization getOrganizationByUMID(String umOrganId) {
		return organizationDao.getOrganizationByUMID(umOrganId);
	}


	public List<Organization> organs = new ArrayList<Organization>();
	@Override
	public void getOrgansBySubCityId(String organOid) {
		List<Organization> list =new ArrayList<Organization>();
		list = organizationDao.getOrgansBySubCityId(organOid);
		if(list.size()>0){
			organs.addAll(list);
			for(Organization o:list){
				getOrgansBySubCityId(o.getUm_organ_id());
			}
		}
		
	}
 
	 
}
